﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Allegro.Framework
{
    /// <summary>
    /// Error handler
    /// </summary>
    public class ErrorHandler
    {
        private static TraceLevel MinTraceLevel = TraceLevel.Error;

        /// <summary>
        /// 
        /// </summary>
        private static int MinLevel = NumericLevel(TraceLevel.Error);

        private static int ErrorLevel = NumericLevel(TraceLevel.Error);
        private static int WarningLevel = NumericLevel(TraceLevel.Warning);
        private static int InfoLevel = NumericLevel(TraceLevel.Info);
        private static int VerboseLevel = NumericLevel(TraceLevel.Verbose);
        private static int OffLevel = NumericLevel(TraceLevel.Off);

        ///<summary>
        ///</summary>
        ///<param name="msg"></param>
        ///<exception cref="ApplicationException"></exception>
        public static void Error(string msg)
        {
            string traceMsg = string.Format("ERROR: {0}", msg);
            if (ErrorLevel >= MinLevel)
            {
                Console.WriteLine(traceMsg);
                Trace.TraceError(traceMsg);
            }
            throw new ApplicationException(traceMsg);
        }
        ///<summary>
        ///</summary>
        ///<param name="msg"></param>
        public static void Warning(string msg)
        {
            if (WarningLevel >= MinLevel)
            {
                string traceMsg = string.Format("WARNING: {0}", msg);
                Console.WriteLine(traceMsg);
                Trace.TraceWarning(traceMsg);
            }
        }
        ///<summary>
        ///</summary>
        ///<param name="msg"></param>
        public static void Info(string msg)
        {
            string traceMsg = string.Format("INFO: {0}", msg);
            Console.WriteLine(traceMsg);
            Trace.TraceInformation(traceMsg);
        }
        static bool DoTrace(TraceLevel level)
        {
            bool doTrace = false;
            int n = NumericLevel(level);
            doTrace = (n  >= MinLevel);
            return doTrace;
        }
        ///<summary>
        ///</summary>
        ///<param name="level"></param>
        ///<returns></returns>
        public static int NumericLevel(TraceLevel level)
        {
            int n = 0;

            switch(level)
            {
                case TraceLevel.Verbose:
                    n = 0;
                    break;
                case TraceLevel.Info:
                    n = 1;
                    break;
                case TraceLevel.Warning:
                    n = 2;
                    break;
                case TraceLevel.Error:
                    n = 3;
                    break;
                case TraceLevel.Off:
                    n = 4;
                    break;
            }
            return n;
        }

        static void SetMinimumTraceLevel(TraceLevel level)
        {
            MinTraceLevel = level;
            MinLevel = NumericLevel(level);
        }
        static TraceLevel GetMinimumTraceLevel()
        {
            return MinTraceLevel;
        }
    }
}
